﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PointerClock
{
    /// <summary>
    /// 时种类，封装时钟所有属性
    /// </summary>
    public class Clock
    {
        #region 属性


        /// <summary>
        /// 记录小时
        /// </summary>
        public int Hour;
        /// <summary>
        /// 记录分钟
        /// </summary>
        public int Minute;
        /// <summary>
        /// 记录秒数
        /// </summary>
        public int Second;
        /// <summary>
        /// 时钟中点坐标
        /// </summary>
        public Point Center_P;
        /// <summary>
        /// 小时坐标
        /// </summary>
        public Point Hour_P;
        /// <summary>
        /// 分钟坐标
        /// </summary>
        public Point Minute_P;
        /// <summary>
        /// 秒钟坐标
        /// </summary>
        public Point Second_P;
        /// <summary>
        /// 闹钟路径
        /// </summary>
        private string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"resources\chimes.wav";//.wav音频文件路径
        /// <summary>
        /// SoundPlayer实例
        /// </summary>
        private System.Media.SoundPlayer player;
        #endregion

        #region 构造函数


        /// <summary>
        /// 构造函数
        /// 初始化时钟
        /// 读取当前的时间给时钟，计算当前时钟指针坐标
        /// </summary>
        public Clock()
        {
            Hour = DateTime.Now.Hour;//当前小时
            Minute = DateTime.Now.Minute; //当前分钟
            Second = DateTime.Now.Second; //当前秒钟
            Center_P = new Point(140, 130);  //时钟中心坐标，常量
            CalcPoint();
            player = new System.Media.SoundPlayer(path);
        }
        #endregion

        #region 方法


        /// <summary>
        /// 计算坐标
        /// </summary>
        private void CalcPoint()
        {
            //秒针远端坐标
            Second_P = new Point(Convert.ToInt32(Center_P.X + 100 * Math.Sin(Second * Math.PI / 30.0)), Convert.ToInt32(Center_P.Y - 100 * Math.Cos(Second * Math.PI / 30.0)));
            //分钟远端坐标
            Minute_P = new Point(Convert.ToInt32(Center_P.X + 75 * Math.Sin(Minute * Math.PI / 30.0 + Second * Math.PI / 1800.0)), Convert.ToInt32(Center_P.Y - 75 * Math.Cos(Minute * Math.PI / 30.0 + Second * Math.PI / 1800.0)));
            //时针远端坐标
            Hour_P = new Point(Convert.ToInt32(Center_P.X + 50 * Math.Sin((Hour * 30.0 + Minute * 0.5 + Second / 120.0) / 360.0 * 2 * Math.PI)), Convert.ToInt32(Center_P.Y - 50 * Math.Cos((Hour * 30.0 + Minute * 0.5 + Second / 120.0) / 360.0 * 2 * Math.PI)));
        }

        /// <summary>
        /// 计算时间方法
        /// </summary>
        public void CalculateTime()
        {
            Second++;  //秒数加1，重新计算小时数和分钟数

            if (Second == 60)//到60秒归零
            {
                Second = 0;
                Minute++;
                if (Minute == 60) //到60分钟归零
                {
                    Minute = 0;
                    Hour++;
                    if (Hour == 12)  //到12点归零
                    {
                        Hour = 0;
                    }
                }
            }
            CalcPoint();
        }
        /// <summary>
        /// 播放闹钟
        /// </summary>
        public void PlayAlarmClock()
        {
            try
            {
                player.PlayLooping();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "指针闹钟");
            }
        }
        /// <summary>
        /// 停止闹钟
        /// </summary>
        public void StopAlarmClock()
        {
            try
            {
                player.Stop();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "指针闹钟");
            }
        }
        #endregion

    }
}
